package com.example.tse.rbyair.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tse.rbyair.R;
import com.example.tse.rbyair.util.ClearEditText;

import org.kymjs.kjframe.base.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ForgetPwdActivity extends BaseActivity implements OnClickListener {

    @Bind(R.id.topBackTxt)
    TextView topBackTxt;
    @Bind(R.id.topTitleTxt)
    TextView topTitleTxt;
    @Bind(R.id.phoneEtxt)
    ClearEditText phoneEtxt;
    @Bind(R.id.codeEtxt)
    ClearEditText codeEtxt;
    @Bind(R.id.getcodeBtn)
    Button getcodeBtn;
    @Bind(R.id.pwdTxt)
    ClearEditText pwdTxt;
    @Bind(R.id.submitBtn)
    Button submitBtn;
    // 密码是否隐藏
    private boolean isHidden = true;
    //	private Button getCodeButton;
//	private EditText phoneEtxt;
//	private EditText codeEtxt;
//	private EditText pwdTxt;
//	private Button submitBtn;
    private boolean hasgetcode = false;
//	private TextView topBackTxt;

    @Override
    public void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_forget_pwd_one);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
//        getCodeButton = (Button) findViewById(R.id.getcodeBtn);
//        phoneEtxt = (EditText) findViewById(R.id.phoneEtxt);
//        codeEtxt = (EditText) findViewById(R.id.codeEtxt);
//        pwdTxt = (EditText) findViewById(R.id.pwdTxt);
//        submitBtn = (Button) findViewById(R.id.submitBtn);
        submitBtn.setOnClickListener(this);
        getcodeBtn.setOnClickListener(this);
//        topBackTxt = (TextView) findViewById(R.id.topBackTxt);
        topBackTxt.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.topBackTxt:
                this.finish();

            case R.id.getcodeBtn:
//                if (checkPhone()) {
//                    CountDownButtonHelper helper = new CountDownButtonHelper(getcodeBtn, "获取验证码", 60, 1);
//                    helper.setOnFinishListener(new CountDownButtonHelper.OnFinishListener() {
//
//                        @Override
//                        public void finish() {
//
//                        }
//                    });
//                    helper.start();
//
//                    getVerifyCode();
//                }
                break;
            case R.id.submitBtn:
                if (checkRegistInfo()) {
                    forget();
                }
                break;
            default:
                break;
        }
    }

    private boolean checkPhone() {
        String telRegex = "[1][3578]\\d{9}";
        if (!phoneEtxt.getText().toString().trim().matches(telRegex)) {
            Toast.makeText(ForgetPwdActivity.this, R.string.pleaseinputcorrectphone, Toast.LENGTH_LONG).show();
            return false;
        } else {
            return true;
        }
    }

    private void getVerifyCode() {
//		AccountSendVcodeRequest r = new AccountSendVcodeRequest();
//		r.setPhone(phoneEtxt.getText().toString().trim());
//		r.setType("1");
//		RemoteServiceFactory.getInstance().getAccountService(this).sendVcode(r, new RemoteServiceListener<AccountSendVcodeResponse>() {
//
//			@Override
//			public void ok(AccountSendVcodeResponse t) {
//				hasgetcode = true;
//				BaseToast.showCenterToast(R.string.veritycodesendsuccess, false);
//			}
//
//			@Override
//			public void failue(int errorCode, String errorMessage) {
//				BaseToast.showCenterToast(errorMessage, true);
//			}
//		});
    }

    private boolean checkPwd() {
        String s = pwdTxt.getText().toString().trim();
        if (!TextUtils.isEmpty(s) && s.length() >= 6 && s.length() <= 16) {
            return true;
        } else {
            return false;
        }
    }

    private boolean checkRegistInfo() {
        if (hasgetcode) {
            if (TextUtils.isEmpty(codeEtxt.getText().toString().trim())) {
                Toast.makeText(ForgetPwdActivity.this, R.string.pleaseinputveritycode, Toast.LENGTH_LONG).show();
                return false;
            } else if (!checkPwd()) {
                Toast.makeText(ForgetPwdActivity.this, R.string.pleaseinputcorrectpwd, Toast.LENGTH_LONG).show();
                return false;
            } else {
                return true;
            }
        } else {
            Toast.makeText(ForgetPwdActivity.this, R.string.pleasegetveritycodefirst, Toast.LENGTH_LONG).show();
            return false;
        }
    }

    private void forget() {
//		showLoadingDialog();
//		AccountForgetRequest r = new AccountForgetRequest();
//		r.setPhone(phoneEtxt.getText().toString().trim());
//		r.setVcode(codeEtxt.getText().toString().trim());
//		r.setPassword(pwdTxt.getText().toString().trim());
//		RemoteServiceFactory.getInstance().getAccountService(mContext).forget(r,new RemoteServiceListener<AccountForgetResponse>() {
//
//			@Override
//			public void ok(AccountForgetResponse t) {
//				dismissLoadingDialog();
//				BaseToast.showCenterToast(R.string.welldone, false);
//				RbLog.i(t.toString());
//				Intent intent = new Intent(ForgetPwdActivity.this, LoginActivity.class);
//				intent.putExtra("phone", phoneEtxt.getText().toString().trim());
//				startActivity(intent);
//				ForgetPwdActivity.this.finish();
//			}
//
//			@Override
//			public void failue(int errorCode, String errorMessage) {
//				dismissLoadingDialog();
//				BaseToast.showCenterToast(errorMessage, true);
//			}
//
//		});
    }
}
